module zysx {
	export class NotifyMgr {
		private static typeDic: any = {};
		public constructor() {
		}
		public static registerNotify(type: string, callBack: Function, thisObject: any): void {
			if (!this.typeDic[type]) {
				this.typeDic[type] = [{ callback: callBack, thisObject: thisObject }];
			} else {
				let typeList: Array<any> = this.typeDic[type];
				let result: boolean = false;
				for (let item of typeList) {
					if (item.callback == callBack && item.thisObject == thisObject) {
						result = true;
						break;
					}
				}
				if (!result) {
					typeList.push({ callback: callBack, thisObject: thisObject });
					this.typeDic[type] = typeList;
				}
			}
		}
		public static unRegisterNotify(type: string, callBack: Function, thisObject: any): void {
			if (this.typeDic[type]) {
				let typeList: Array<any> = this.typeDic[type];
				for (let item of typeList) {
					if (item.callback == callBack && item.thisObject == thisObject) {
						let index: number = typeList.indexOf(item);
						if (index != -1) {
							typeList.splice(index, 1);
						}
						break;
					}
				}
				this.typeDic[type] = typeList;
			}
		}
		public static sendNotification(type: string, params: any = null): void {
			if (this.typeDic[type]) {
				let typeList: Array<any> = this.typeDic[type];
				for (let item of typeList) {
					if (item.callback) {
						item.callback.call(item.thisObject, params);
					}
				}
			}
		}
		public static removeThisObjectNofity(thisObject: any): void {
			for (let type in this.typeDic) {
				let typeList: Array<any> = this.typeDic[type];
				for (let i: number = typeList.length - 1; i >= 0; i--) {
					if (typeList[i].thisObject == thisObject) {
						typeList.splice(i, 1);
					}
				}
				this.typeDic[type] = typeList;
			}
		}
	}
}